home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 19.zip / BS1 part 19 / AmigaLibDisk 162.adf / Iffar / delete.c < prev    next >
C/C++ Source or Header  |  1988-10-02  |  2KB  |  101 lines

  1. /* iffar - IFF CAT archiver delete functions
  2.  
  3.    By Karl Lehenbauer, version 1.2, release date 5/9/88.
  4.    This code is released to the public domain.
  5.    See the README file for more information.
  6.  
  7. */
  8.  
  9. #include <exec/types.h>
  10. #include <exec/memory.h>
  11. #include <stdio.h>
  12. #include <fcntl.h>
  13. #include "assert.h"
  14. #include "iff.h"
  15.  
  16. extern ULONG nextchunk();
  17.  
  18. int delete_entries(archive_name,fnames,nfiles)
  19. char *archive_name;
  20. char *fnames[];
  21. int nfiles;
  22. {
  23.     int old_archive_fd, new_archive_fd;
  24.     ULONG cat_type, chunkid, innerchunkid, subtype;
  25.     long chunksize, innerchunksize, filesize;
  26.     char textbuf[128], old_archive_name[128];
  27.     int i, delete_file, file_bytes;
  28.  
  29.     extern int verbose;
  30.  
  31.     /* rename the archive to its old name concatenated with ".old"
  32.      */
  33.     sprintf(old_archive_name,"%s.old",archive_name);
  34.     unlink(old_archive_name);
  35.     rename(archive_name,old_archive_name);
  36.  
  37.     if ((old_archive_fd = OpenCAT(old_archive_name,&cat_type,&filesize)) == -1)
  38.     {
  39.         fprintf(stderr,"Can't open archive '%s'\n",old_archive_name);
  40.         return(0);
  41.     }
  42.  
  43.     if ((new_archive_fd = create_archive(archive_name,ID_MISC)) < 0)
  44.         return(0);
  45.  
  46.     while ((chunkid = nextCATchunk(old_archive_fd,&subtype,&textbuf[0],&chunksize,&filesize)) != 0L)
  47.     {
  48.         /* if the chunk type isn't FORM, CAT or LIST, copy it across 
  49.          * without looking at it */
  50.          if (chunkid != ID_FORM && chunkid != ID_CAT && chunkid != ID_LIST)
  51.          {
  52.              if (!WriteChunkHeader(new_archive_fd,chunkid,chunksize))
  53.                 return(0);
  54.             copychunkbytes(old_archive_fd,new_archive_fd,chunksize,&filesize);
  55.         }
  56.  
  57.         /* search to see if this chunk's name is one specified in fnames,
  58.          * an array of pointer to char strings */
  59.  
  60.         delete_file = 0;
  61.         for (i = 0; i < nfiles; i++)
  62.         {
  63.             if (!strnicmp(fnames[i],textbuf, 128))
  64.             {
  65.                 delete_file = 1;
  66.                 break;
  67.             }
  68.         }
  69.         /* if we did decide to delete it, skip it */
  70.         if (delete_file)
  71.         {
  72.             if (verbose)
  73.                 fprintf(stderr,"deleting %s\n",textbuf);
  74.  
  75.                 if (!skipchunk(old_archive_fd,chunksize,&filesize))
  76.                 {
  77.                     fprintf(stderr,"delete: skipchunk failed\n");
  78.                     return(0);
  79.                 }
  80.         }
  81.         else    /* we want to copy it */
  82.         {
  83.             if (!WriteCATentry(new_archive_fd,textbuf,chunkid,subtype,chunksize))
  84.                 return(0);
  85.  
  86.             copychunkbytes(old_archive_fd,new_archive_fd,chunksize,&filesize);
  87.         }
  88.     }
  89.  
  90.     /* write the right length in for the header */
  91.     rewrite_archive_header(new_archive_fd);
  92.  
  93.     /* close the old and new archive files and return success */
  94.     close(old_archive_fd);
  95.     close(new_archive_fd);
  96.     return(1);
  97. }
  98.  
  99. /* end of extract.c */
  100.  
  101.